package com.line.config.auth;

import com.line.config.cons.BasicCons;
import com.line.service.api.IAuthService;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * OAuth2的授权拦截器<br/>
 * 具体的业务在业务层实现
 *
 * @author Administrator
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {

	@Resource(name = "authService")
	private IAuthService authService;

	/**
	 * 所有的请求具体处理的地方<br/>
	 * 返回true，表示请求可继续执行下去<br/>
	 * 返回false,表示请求不可执行下去，直接返回客户端<br/>
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		return authService.preHandle(request, response);
	}

	/**
	 * 当请求完毕之后，将线程内部的缓存清除
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		BasicCons.AUTH_THREADLOCAL.remove(); // 清除缓存信息
		super.afterCompletion(request, response, handler, ex);
	}
}
